//
//  MVVM_TableViewCell.m
//  MVC|MVP|MVVM
//
//  Created by Guo Rui on 2017/11/15.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "MVVM_TableViewCell.h"
#import <RZDataBinding/RZDataBinding.h>
#import <UIImageView+WebCache.h>

@interface MVVM_TableViewCell()
@property (weak, nonatomic) IBOutlet UIButton *comentButton;
@property (weak, nonatomic) IBOutlet UIButton *likeButton;
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
@property (weak, nonatomic) IBOutlet UIImageView *albumView;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageView_height_constraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageView_width_constraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageView_top_constraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentLabel_top_constraint;
@end


@implementation MVVM_TableViewCell

- (void)setVm:(NSObject<ViewModelProtocol> *)vm
{
    _vm = vm;
    [self bind];
    
    _vm.imageUrl = vm.imageUrl;
    _vm.hiddenImageView = vm.hiddenImageView;
    _vm.likeCountDesc = vm.likeCountDesc;
    _vm.commentCountDesc = vm.commentCountDesc;
}

- (void)bind
{
    [self.vm rz_addTarget:self action:@selector(imageUrlDidChanged:) forKeyPathChange:RZDB_KP_PROTOCOL(ViewModelProtocol, imageUrl)];
    [self.vm rz_addTarget:self action:@selector(hiddenImageDidChanged:) forKeyPathChange:RZDB_KP_PROTOCOL(ViewModelProtocol, hiddenImageView)];
    [self.vm rz_addTarget:self action:@selector(likeCountDidChanged:) forKeyPathChange:RZDB_KP_PROTOCOL(ViewModelProtocol, likeCountDesc)];
    [self.vm rz_addTarget:self action:@selector(commentCountDidChanged:) forKeyPathChange:RZDB_KP_PROTOCOL(ViewModelProtocol, commentCountDesc)];
    
    [self.contentLabel rz_bindKey:RZDB_KP(UILabel, text) toKeyPath:RZDB_KP_PROTOCOL(ViewModelProtocol, timelineContent) ofObject:self.vm];
    [self.contentLabel_top_constraint rz_bindKey:RZDB_KP(NSLayoutConstraint, constant) toKeyPath:RZDB_KP_PROTOCOL(ViewModelProtocol, hiddenTimelineContentLabel) ofObject:self.vm withTransform:^id(NSNumber *value) {
        BOOL hidden = value.boolValue;
        return hidden ? @(0) : @20;
    }];
    
    [self.infoLabel rz_bindKey:RZDB_KP(UILabel, text) toKeyPath:RZDB_KP_PROTOCOL(ViewModelProtocol, publishInfo) ofObject:self.vm];
    [self.likeButton rz_bindKey:RZDB_KP(UIButton, selected) toKeyPath:RZDB_KP_PROTOCOL(ViewModelProtocol, isLike) ofObject:self.vm];
}

- (void)likeCountDidChanged:(NSDictionary *)desc
{
    [self.likeButton setTitle:desc[kRZDBChangeKeyNew] forState:UIControlStateNormal];
    [self.likeButton setTitle:desc[kRZDBChangeKeyNew] forState:UIControlStateSelected];
}

- (void)commentCountDidChanged:(NSDictionary *)desc
{
    [self.comentButton setTitle:desc[kRZDBChangeKeyNew] forState:UIControlStateNormal];
    [self.comentButton setTitle:desc[kRZDBChangeKeyNew] forState:UIControlStateSelected];
}

- (void)imageUrlDidChanged:(NSDictionary *)change
{
    NSString *imageUrl = change[kRZDBChangeKeyNew];
    [self.albumView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
}

- (void)hiddenImageDidChanged:(NSDictionary *)change
{
    BOOL hidden = [change[kRZDBChangeKeyNew] boolValue];
    if (hidden) {
        self.imageView_top_constraint.constant = 0;
        self.imageView_height_constraint.constant = 0;
    }
    else {
        self.imageView_top_constraint.constant = 10.f;
        self.imageView_height_constraint.constant = self.vm.imageSize.height;
        self.imageView_width_constraint.constant = self.vm.imageSize.width;
    }
}

- (IBAction)didClickLikeButton:(id)sender {
    [self.vm clickZan];
}

@end
